package com.yitong.equipment.network.oss

import android.os.Parcel
import android.os.Parcelable
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.yitong.equipment.constant.Constants

fun OssConfig(
    OSSBase: String,
    OSSBucketName: String,
    OSSEndpoint: String,
    OSSPrefix: String,
    OSSSignatureEndpoint: String
) {
    val ossModel = OssBean(OSSBase, OSSBucketName, OSSEndpoint, OSSPrefix, OSSSignatureEndpoint)
    SPUtils.getInstance().put(Constants.SharePreKey.KEY_OSS_CONFIG, GsonUtils.toJson(ossModel))
}

fun getOssConfig(): OssBean {
    return GsonUtils.fromJson(
        SPUtils.getInstance().getString(Constants.SharePreKey.KEY_OSS_CONFIG),
        OssBean::class.java
    )
}

data class OssBean(
    var OSSBase: String? = "http://yt-temp.img-cn-hangzhou.aliyuncs.com",
    var OSSBucketName: String? = "yt-temp",
    var OSSEndpoint: String? = "oss-cn-hangzhou.aliyuncs.com",
    var OSSPrefix: String? = "",
    var OSSSignatureEndpoint: String? = "https://api.yitong.com/aliyun-oss-file-service/v1/oss/signature"
) : Parcelable {

    constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString()
    ) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(OSSBase)
        parcel.writeString(OSSBucketName)
        parcel.writeString(OSSEndpoint)
        parcel.writeString(OSSPrefix)
        parcel.writeString(OSSSignatureEndpoint)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<OssBean> {
        override fun createFromParcel(parcel: Parcel): OssBean {
            return OssBean(parcel)
        }

        override fun newArray(size: Int): Array<OssBean?> {
            return arrayOfNulls(size)
        }
    }


}